package com.whw.springboot.service.impl;

import com.whw.springboot.entity.*;
import com.whw.springboot.mapper.ClockMapper;
import com.whw.springboot.service.ClockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClockServiceImpl implements ClockService {
    @Autowired
    private ClockMapper clockMapper;

    @Override
    public int deleteByPrimaryKey(Integer clockId) {
        // TODO Auto-generated method stub
        return clockMapper.deleteByPrimaryKey(clockId);
    }

    @Override
    public int insert(Clock record) {
        // TODO Auto-generated method stub
        return clockMapper.insert(record);
    }

    @Override
    public int insertSelective(Clock record) {
        // TODO Auto-generated method stub
        return clockMapper.insertSelective(record);
    }

    @Override
    public List<Clock> selectByPrimaryKey(ClockNumber clockNumber) {
        // TODO Auto-generated method stub
        return clockMapper.selectByPrimaryKey(clockNumber);
    }

    @Override
    public int updateByPrimaryKeySelective(Clock record) {
        // TODO Auto-generated method stub
        return clockMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Clock record) {
        // TODO Auto-generated method stub
        return clockMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<ReportTemp> queryTemp(String user_id, String date) {
        List<UserDor> queryUserId = clockMapper.queryUserId(user_id);
        List<ReportTemp> reportTemps = new ArrayList<>();
        for (UserDor userDor : queryUserId) {
            ReportTemp reportTemp = new ReportTemp();

            List<UserTemp> queryTemp = clockMapper.queryTemp(userDor.getUser_id());
            List<UserTemp> queryTemps = new ArrayList<UserTemp>();
//			System.out.println(queryTemp.get(0));
            for (int i = 0; i < queryTemp.size(); i++) {

                SimpleDateFormat bjSdf = new SimpleDateFormat("yyyy-MM-dd");
                String time= bjSdf.format( queryTemp.get(i).getClock_time());

                if (time.equals(date)) {
                    System.out.println(queryTemp.get(i));
                    int j=0;
                    while (i < queryTemp.size()) {

                        queryTemps.add(queryTemp.get(i));
                        j++;

                        i++;
                        if (j>=5) {
                            break;
                        }
                    }

                }
            }
            reportTemp.setUserTemps(queryTemps);
            reportTemp.setUser_name(userDor.getUser_name());
            reportTemp.setDormitory_build_id(userDor.getDormitory_build_id());
            reportTemp.setDormitory_house_id(userDor.getDormitory_house_id());
            reportTemps.add(reportTemp);
        }

        return reportTemps;

    }
    }

